package sjtu;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Scanner;
/**
 * 5
 * 1 1 3 6 8
 * 1 2 2 5 5
 * 4 4 0 3 3
 * 8 0 2 3 4
 * 4 3 0 2 1
 * @author chenxiaokang
 */
public class Sjtu1031 {
	public static void main(String[] args) {
		new Sjtu1031().go();
	}
	
	public void go(){
		Scanner scan = new Scanner(System.in);
		int n,x,y,x1,y2;
		int[][] arr;
		List<Integer> list = new ArrayList<>();
		Queue<String> queue = new LinkedList<>();
		while(scan.hasNext()){
			n = scan.nextInt();
			arr = new int[n][n];
			x1 = 0;
			y2 = 0;
			x = 0;
			y = 0;
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < n; j++) {
					arr[i][j] = scan.nextInt();
				}
			}
			queue.add("0,0");
			while(!(x == (n - 1) && y == (n - 1))){
				String postion = queue.poll();
				x = Integer.parseInt(postion.split(",")[0]);
				y = Integer.parseInt(postion.split(",")[1]);
				System.out.println("x:"+x);
				System.out.println("y:"+y);
				list.add(arr[x][y]);
				if(x + 1 < n){
					x1 = x + 1;
				}else if(y + 1 < n){
					y2 = y + 1;
				}
				System.out.println("x1:"+x1);
				System.out.println("y2:"+y2);
				if(arr[x1][y] > arr[x][y2]){
					queue.add(x + "," + y2);
				}else if(arr[x1][y] < arr[x][y2]){
					queue.add(x1 + "," + y);
				}else{
					queue.add(x + "," + y2);
					queue.add(x1 + "," + y);
				}
				for(String str : queue){
					System.out.print(str+" ");
				}
			}
			for(int i : list){
				System.out.println(i);
			}
			list.clear();
		}
	}
}
